import {
  Injectable,
  ExecutionContext,
  UnauthorizedException
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
  constructor() {
    super();
  }
  canActivate(context: ExecutionContext) {
    const request = context.switchToHttp().getRequest();
    if (!request.body.username) {
      throw new UnauthorizedException('用户名不能为空');
    }
    if (!request.body.password) {
      throw new UnauthorizedException('密码不能为空');
    }
    return super.canActivate(context);
  }
}
